Hello I am having issues where when I open a link to YouTube in a wkwebview it opens up the installed YouTube app. I have tried my own way of blocking the opening of the YouTube app but it seems I'm stuck.
Is there a way to prevent certain apps from opening automatically from links clicked on or traveled to?
Here is what I have so far.
guard let url = navigationAction.request.url else {
decisionHandler(.cancel)
return
}
//not sure why this code is not being used...
// Check if the navigation is a link click with target="_blank"
if navigationAction.targetFrame == nil {
// Handle the navigation request to open a new window
if let url = navigationAction.request.url {
// UIApplication.shared.open(url) //opens in safari....
let request = URLRequest(url: url)
webView.load(request)
decisionHandler(.cancel)
return
}
}
// Continue with the navigation if not a link with target="_blank"
if url.scheme == "youtube" || url.scheme == "music" {
decisionHandler(.cancel)
return
}
//this should cancel app opening for youtube:// links and apple music.
// if let url = navigationAction.request.url {
// // Allow Google sign-in redirects
// if url.absoluteString.contains("accounts.google.com") || url.absoluteString.contains("gstatic.com") {
// decisionHandler(.allow)
// return
// }
// }
if url.absoluteString.hasPrefix("http") {
decisionHandler(.allow)
return
}
if let url = navigationAction.request.url {
if shouldDownloadFile(from:url) {
// if navigationAction.navigationType == .other, let mimeType = navigationAction.request.allHTTPHeaderFields?["Content-Type"], mimeType.contains("application/pdf") {
downloadFile (from: url)
decisionHandler(.cancel)
return
}
}
decisionHandler(.allow)
}
Post
Replies
Boosts
Views
Activity
Hello, a very simple question, is there a test domain that is flagged by safari as a fraudulent webpage? one that is just for testing? Thank you.
So I am trying to control the wkwebview content within my app. When I try the following code>
var backButton: some View {
Button(action: {
webView.goBack()
// Add your back button action here
}, label: {
Image(systemName: "arrow.uturn.backward")
.font(.title)
})
.navigationBarBackButtonHidden(true)
}
I get webView out of scope.
This is how I have webView implimented
struct WebView: UIViewRepresentable {
let url: URL
let preferences = WKPreferences()
let useragent = WKWebViewConfiguration()
func makeUIView(context: Context) -> WKWebView {
@State var webView = WKWebView()
let request = URLRequest(url: url)
useragent.applicationNameForUserAgent = "Version/1.2 LavaRock/1.2"
useragent.allowsAirPlayForMediaPlayback = true
useragent.allowsInlineMediaPlayback = true
useragent.allowsPictureInPictureMediaPlayback = true
//
preferences.isFraudulentWebsiteWarningEnabled = true
preferences.isSiteSpecificQuirksModeEnabled = true
preferences.isElementFullscreenEnabled = true
webView.allowsBackForwardNavigationGestures = true
// webView.configuration.allowsPictureInPictureMediaPlayback = true
webView.customUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Version/1.2 LavaRock/1.2"
webView.load(request)
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
// Handle view updates if needed
}
}
How should create the button?
Hello, I'm Trying to learn swift and looking for resources to learn swift UI and try to create a basic web browser app for ios and mac. (using webkit). Does anyone have any type of tips and tricks to perform this? Thank you ahead of time.